import os

import pandas as pd
import xlrd

from zc_getdata.config import Config

DATAPATH = 'C:/Users/hp/PycharmProjects/spider/data/zc_pInfo/'


def read_Pinfo(fname='pinfo_99994.xls', path=DATAPATH) -> dict:
    workbook = xlrd.open_workbook_xls(filename=path + fname)
    bids_sheet = workbook.sheet_by_name('bids_sheet')
    project_sheet = workbook.sheet_by_name('project_sheet')
    p = {}
    p['pid'] = project_sheet.row_values(0)[1]
    p['state'] = project_sheet.row_values(1)[1]
    p['cost'] = project_sheet.row_values(2)[1]
    p['timecost'] = project_sheet.row_values(3)[1]
    # 发包方
    p['owner'] = project_sheet.row_values(4)[1]
    # 接包方 使用set去重
    p['contractor_name'] = project_sheet.row_values(5)[1]
    p['contractor_url'] = project_sheet.row_values(6)[1]
    p['pname'] = project_sheet.row_values(7)[1]
    datestr = project_sheet.row_values(8)[1]
    p['date'] = datestr
    p['description'] = project_sheet.row_values(9)[1]
    p['win'] = ''
    p['rate'] = ''
    bidsnum = bids_sheet.nrows
    bids = []
    b = {}
    for i in range(1, bidsnum):
        bidinfo = bids_sheet.row_values(i)
        for j in range(len(Config.bid_fields)):
            field = Config.bid_fields[j]
            b[field] = bidinfo[Config.bid_index[field]]
        if bidinfo[Config.bid_index['win']] == 1:
            p['win'] = bidinfo[Config.bid_index['uid']]
        if b['uid'] == '被冻结':
            b['uid'] = '00000'
        bids.append(b)
    p['bids'] = bids
    # if (p['win'] != ''):
    #     rate = getProjectData.get_rate_From_bids(p['win'], p['pid'])
    #     p['rate'] = rate
    return p


def read_bids(fname='pinfo_99994.xls', path=DATAPATH) -> dict:
    bids = pd.read_excel(path + fname, sheet_name='bids_sheet')
    return bids


def get_imlist():
    return [os.path.join(f) for f in os.listdir(DATAPATH) if f.endswith('.xls')]


def get_All_bids_info():
    pinfo_namelist = get_imlist()
    bidslist = pd.DataFrame()
    for fname in pinfo_namelist:
        bids = read_bids(fname)
        bidslist = pd.concat([bidslist,bids],0)
        bids[bids['uname'] == 0] = 0
    bidslist = bidslist.drop_duplicates(subset=['uid'], keep='first')
    bidslist.to_csv('allbidsinfo_zc.csv',encoding='utf_8_sig',index=None)
    return bidslist


def get_All_Pinfo_Data(timeFrom='2015', state=''):
    pinfo_namelist = get_imlist()
    plist = []
    states = []
    for pname in pinfo_namelist:
        p = {}
        p = read_Pinfo(pname)
        plist.append(p)
        if not (states.__contains__(p['state'])):
            states.append(p['state'])
    print(states)
    return plist

if __name__ == '__main__':
    # infolist = get_All_Pinfo_Data()
    # datas = pd.DataFrame(infolist)
#     datas.to_excel('pinfo_sum.xls')
#     # csv = pd.read_excel(topsis_config.Data_Dir + 'test.xlsx', sheet_name='Sheet2')
#
    # d_fin = datas[datas['contractor_name'] != ""]
    # d_fin_2017 = d_fin[d_fin['date'] > '2017']
    # d_fin_2017_ex = d_fin_2017[d_fin_2017['win'] != "被冻结"]
    # d_fin_2017_ex.to_excel('d_fin_2017_ex.xls')


    res = get_All_bids_info()
    print(len(res))